/*
 * 广州云徙科技有限公司源代码，版权归广州云徙科技有限公司所有。
 * 
 * 项目名称 : yxs-gateway
 * 创建日期 : 2017年3月28日
 * 修改历史 : 
 *     1. [2017年3月28日]创建文件 by tan.wenjuan
 */
package com.project.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dtyunxi.rest.PageData;
import com.dtyunxi.rest.RestCode;
import com.dtyunxi.rest.RestResponse;
import com.project.common.Constants;
import com.project.entity.atricle.Article;
import com.project.entity.atricle.Category;
import com.project.entity.user.User;
import com.project.service.IArticleService;
import com.project.util.EhcacheUtil;
import com.project.util.LoginUserUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 文章管理
 * @author tan.wenjuan
 */
@RestController
@RequestMapping("api/article")
@Api(value = "api/article", description = "文章管理")
public class ArticleController {
    @Autowired
    private IArticleService articleService;
    
    @ApiOperation(value = "获取类别列表", notes = "获取类别列表",response = Category.class)
    @RequestMapping(value = "category/list", method = RequestMethod.GET)
    public RestResponse categoryList(){
        return new RestResponse(articleService.getGategoryList());
    }
    
    @ApiOperation(value = "根据父id获取类别列表", notes = "根据父id获取类别列表",response = Category.class)
    @RequestMapping(value = "category/ops/{id}", method = RequestMethod.GET)
    public RestResponse categoryListByParentId(@ApiParam(required = true, name = "id", value = "类别id") @PathVariable Long id){
        return new RestResponse(articleService.getGategoryListByParentId(id));
    }
    
    @ApiOperation(value = "根据类别id获取文章列表", notes = "根据类别id获取文章列表",response = Article.class)
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public RestResponse articleListByCategoryId(@ApiParam(required = false, name = "id", value = "类别id") @RequestParam(value = "id", required = false) Long id,
            @RequestParam(defaultValue = "1", required = false) int pageNo,
            @RequestParam(defaultValue = "5", required = false) int pageSize){
        PageData page = new PageData();
        page.setCurrentPage(pageNo);
        page.setPageSize(pageSize);
        page.setData(articleService.getArticleListByCategoryId(id,pageNo,pageSize));
        return new RestResponse(page);
    }
    
    @ApiOperation(value = "根据标签id获取文章列表", notes = "根据标签id获取文章列表",response = Article.class)
    @RequestMapping(value = "listByLabelId", method = RequestMethod.GET)
    public RestResponse articleListByLabelId(@ApiParam(required = false, name = "id", value = "标签id") @RequestParam(value = "id", required = false) Long id,
            @RequestParam(defaultValue = "1", required = false) int pageNo,
            @RequestParam(defaultValue = "5", required = false) int pageSize){
        PageData page = new PageData();
        page.setCurrentPage(pageNo);
        page.setPageSize(pageSize);
        page.setData(articleService.getArticleListByLabelId(id,pageNo,pageSize));
        return new RestResponse(page);
    }
    
    @ApiOperation(value = "文章详情", notes = "文章详情",response = Article.class)
    @RequestMapping(value = "/ops/{id}", method = RequestMethod.GET)
    public RestResponse articleDetail(@ApiParam(required = true, name = "id", value = "文章id") @PathVariable Long id,
    		HttpServletRequest request, HttpServletResponse response){
        User user = LoginUserUtil.getLoginUser(request, response);
        //文章浏览量加1
        articleService.updatePageView(id);
        
        Article article = articleService.getArticleDetail(id);
        if(user!=null){
            //判断该文章是否被当前用户收藏
            if(articleService.isCollection(id, user.getId())){
                article.setCollection(1);
            }else{
                article.setCollection(0);
            }
        }
        return new RestResponse(article);
    }
    
    @ApiOperation(value = "根据类别获取最新5篇文章的封面图", notes = "根据类别获取最新5篇文章的封面图",response = String.class)
    @RequestMapping(value = "/articlePhotoList", method = RequestMethod.GET)
    public RestResponse articlePhotoList(@ApiParam(required = false, name = "id", value = "类别id") @RequestParam(value = "id", required = false)Long id){
        return new RestResponse(articleService.getArticlePhotoList(id));
    }
    
    @ApiOperation(value = "根据标签获取最新5篇文章的封面图", notes = "根据标签获取最新5篇文章的封面图",response = String.class)
    @RequestMapping(value = "/articlePhotoListByLabelId", method = RequestMethod.GET)
    public RestResponse articlePhotoListByLabelId(@ApiParam(required = false, name = "id", value = "标签id") @RequestParam(value = "id", required = false)Long id){
        return new RestResponse(articleService.getArticlePhotoListByLabelId(id));
    }
    
    @ApiOperation(value = "收藏文章", notes = "收藏文章",response = String.class)
    @RequestMapping(value = "/collectArticle", method = RequestMethod.GET)
    public RestResponse collectArticle(@ApiParam(required = true, name = "id", value = "文章id") @NotEmpty(message = "文章id不能为空") @RequestParam(value = "id", required = true) Long id,HttpSession session){
        User user = (User)session.getAttribute(Constants.CURRENT_LOGIN_USER);
        if(user==null){
            return new RestResponse(RestCode.RESULT_NOT_LOGIN_ERROR,"请先登录！");
        }else{
            articleService.collectArticle(id, user.getId());
            return new RestResponse(RestCode.RESULT_SUCCESS,"收藏成功！");
        }
        
    }
    
    @ApiOperation(value = "取消收藏", notes = "取消收藏",response = String.class)
    @RequestMapping(value = "/cancelCollect", method = RequestMethod.GET)
    public RestResponse cancelCollect(@ApiParam(required = true, name = "id", value = "文章id")@NotEmpty(message = "文章id不能为空") @RequestParam(value = "id", required = true)Long id,HttpSession session){
        User user = (User)session.getAttribute(Constants.CURRENT_LOGIN_USER);
        if(user==null){
            return new RestResponse(RestCode.RESULT_NOT_LOGIN_ERROR,"请先登录！");
        }else{
            articleService.cancelCollect(id, user.getId());
            return new RestResponse(RestCode.RESULT_SUCCESS,"已取消收藏！");
        }
        
    }
    
    @ApiOperation(value = "文章收藏列表", notes = "文章收藏列表",response = Article.class)
    @RequestMapping(value = "/collectList", method = RequestMethod.GET)
    public RestResponse collectList(HttpSession session,
            @RequestParam(defaultValue = "1", required = false) int pageNo,
            @RequestParam(defaultValue = "5", required = false) int pageSize){
        User user = (User)session.getAttribute(Constants.CURRENT_LOGIN_USER);
        if(user==null){
            return new RestResponse(RestCode.RESULT_NOT_LOGIN_ERROR,"请先登录！");
        }else{
            int count = articleService.collectCount(user.getId());
            PageData page = new PageData();
            page.setCurrentPage(pageNo);
            page.setPageSize(pageSize);
            page.setData(articleService.collectList(user.getId(), pageSize, pageNo));
            
//            page.setCount(count);
            page.setTotal(count);
            if(count>=pageSize){
                page.setPageCount(pageSize);
            }else{
                page.setPageCount(count);
            }
            return new RestResponse(page);
        }
        
    }
    
    @ApiOperation(value = "文章列表(根据标题查询)", notes = "文章列表(根据标题查询)",response = Article.class)
    @RequestMapping(value = "/articleList", method = RequestMethod.GET)
    public RestResponse articleList(@ApiParam(required = false, name = "title", value = "文章标题") @RequestParam(value = "title", required = false) String title,
            @RequestParam(defaultValue = "1", required = false) int pageNo,
            @RequestParam(defaultValue = "5", required = false) int pageSize){
        if(title==null){
            title="";
        }
        int count = articleService.articleCount(title);
        PageData page = new PageData();
        page.setCurrentPage(pageNo);
        page.setPageSize(pageSize);
        
        page.setData(articleService.getArticleListByTitle(title.trim(), pageSize, pageNo));
        
//        page.setCount(count);
        page.setTotal(count);
        if(count>=pageSize){
            page.setPageCount(pageSize);
        }else{
            page.setPageCount(count);
        }
        return new RestResponse(page);
        
    }
}
